var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "maishu-ui-toolkit", "react", "mobile/services/shoppingService", "mobile/services/accountService", "mobile/site", "controls/spiner-component", "mobile/services/weixinService"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const ui = require("maishu-ui-toolkit");
    const React = require("react");
    const shoppingService_1 = require("mobile/services/shoppingService");
    const accountService_1 = require("mobile/services/accountService");
    const site_1 = require("mobile/site");
    const spiner_component_1 = require("controls/spiner-component");
    const weixinService_1 = require("mobile/services/weixinService");
    class PurchasePage extends spiner_component_1.SpinerComponent {
        constructor(props) {
            super(props);
            this.state = { payType: 'balance' };
            this.app = this.props.app;
            this.shopping = this.props.createService(shoppingService_1.ShoppingService);
            this.accout = this.props.createService(accountService_1.AccountService);
            this.weixin = this.props.createService(weixinService_1.WeiXinService);
        }
        balancePurchase(order) {
            console.assert(order.Sum != null);
            console.assert(order.Sum == order.Amount + order.Freight);
            return this.accout.payOrder(order.Id, order.Sum);
        }
        weixinPurchase(order) {
            return __awaiter(this, void 0, void 0, function* () {
                console.assert(order.Sum != null);
                console.assert(order.Sum == order.Amount + order.Freight);
                let openid = this.weixin.openid();
                console.assert(openid != null);
                let args = yield this.weixin.getPayArgs(openid, order);
                WeixinJSBridge.invoke('getBrandWCPayRequest', args, function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        window.location.href = '#Shopping/OrderList';
                    }
                });
            });
        }
        loadData() {
            return __awaiter(this, void 0, void 0, function* () {
                let order = yield this.shopping.order(this.props.data.id);
                return { order };
            });
        }
        pay() {
            let order = this.state.order;
            if (this.state.payType == "weixin") {
                return this.weixinPurchase(order);
            }
            return this.balancePurchase(order).then(() => {
                this.app.redirect('shopping/orderList');
            });
        }
        render() {
            let { order, payType } = this.state;
            return React.createElement(React.Fragment, null,
                React.createElement("header", null, site_1.defaultNavBar(this.props.source, {
                    title: '订单概况',
                })),
                React.createElement("section", null,
                    React.createElement("div", { className: "container" },
                        React.createElement("div", { className: "row", style: { paddingBottom: 10 } },
                            React.createElement("label", { className: "col-xs-3", style: { paddingRight: 0 } }, "\u8BA2\u5355\u72B6\u6001"),
                            React.createElement("div", { className: "col-xs-9", style: { color: '#f70' } }, this.shopping.orderStatusText(order.Status))),
                        React.createElement("div", { className: "row", style: { paddingBottom: 10 } },
                            React.createElement("label", { className: "col-xs-3", style: { paddingRight: 0 } }, "\u8BA2\u5355\u7F16\u53F7"),
                            React.createElement("div", { className: "col-xs-9" }, order.Serial)),
                        React.createElement("div", { className: "row", style: { paddingBottom: 10 } },
                            React.createElement("label", { className: "col-xs-3", style: { paddingRight: 0 } }, "\u8BA2\u5355\u603B\u8BA1"),
                            React.createElement("div", { className: "col-xs-9 price" },
                                "\uFFE5",
                                order.Sum.toFixed(2))),
                        React.createElement("div", { className: "row", style: { paddingBottom: 10 } },
                            React.createElement("label", { className: "col-xs-3", style: { paddingRight: 0 } }, "\u6536\u8D27\u4FE1\u606F"),
                            React.createElement("div", { className: "col-xs-9" }, order.ReceiptAddress)),
                        React.createElement("div", { className: "row", style: { paddingBottom: 10 } },
                            React.createElement("label", { className: "col-xs-3", style: { paddingRight: 0 } }, "\u53D1\u7968\u4FE1\u606F"),
                            React.createElement("div", { className: "col-xs-9" }, order.Invoice)),
                        React.createElement("div", { className: "row", style: { paddingBottom: 10 } },
                            React.createElement("label", { className: "col-xs-3", style: { paddingRight: 0 } }, "\u4E0B\u5355\u65F6\u95F4"),
                            React.createElement("div", { className: "col-xs-9", "data-bind": "text:['{0:g}', order.OrderDate]" }, site_1.formatDate(order.OrderDate))),
                        order.Remark ?
                            React.createElement("div", { "data-bind": "visible:ko.unwrap(order.Remark)", className: "row", style: { paddingBottom: 10 } },
                                React.createElement("label", { className: "col-xs-3", style: { paddingRight: 0 } }, "\u8BA2\u5355\u5907\u6CE8"),
                                React.createElement("div", { className: "col-xs-9" }, order.Remark)) : null,
                        React.createElement("div", { style: { marginBottom: 10 } }, "\u63D0\u793A\uFF1A\u8BF7\u5728\u4E0B\u535524\u5C0F\u65F6\u5185\u4ED8\u6B3E\uFF0C\u8FC7\u671F\u540E\u8BA2\u5355\u5C06\u81EA\u52A8\u53D6\u6D88\u3002"),
                        React.createElement("hr", { className: "row" }),
                        React.createElement("button", { className: payType == "balance" ? "cust-prop selected" : "cust-prop", onClick: e => this.setState({ payType: "balance" }) }, "\u4F59\u989D\u652F\u4ED8"),
                        React.createElement("button", { className: payType == "weixin" ? "cust-prop selected" : "cust-prop", onClick: e => this.setState({ payType: "weixin" }) }, "\u5FAE\u4FE1\u652F\u4ED8"))),
                React.createElement("footer", null, order.Status == 'WaitingForPayment' ?
                    React.createElement("div", { className: "container" },
                        React.createElement("div", { className: "form-group" },
                            React.createElement("button", { className: "btn btn-block btn-primary", ref: (o) => {
                                    if (!o)
                                        return;
                                    o.onclick = ui.buttonOnClick(() => this.pay());
                                } }, "\u7ACB\u5373\u652F\u4ED8"))) : null));
        }
    }
    exports.default = PurchasePage;
});
// shopping.order(page.data.values["id"]).then(order => {
//     ReactDOM.render(<PurchasePage order={order} />, page.element);
// })
